package com.lyxedu.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyxedu.common.Result;
import com.lyxedu.pojo.Menu;
import com.lyxedu.pojo.Role;
import com.lyxedu.services.MenuService;
import com.lyxedu.services.RoleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    @GetMapping
    public Result list(){
        return Result.success(menuService.list());
    }

    @PostMapping("/delete")
    public Result delete(int id){
        boolean b = menuService.removeById(id);
        return Result.success();
    }

    @PostMapping("/delBatch")
    public Result delete(@RequestBody Integer[] ids){
        List<Integer> list = Arrays.asList(ids);
        boolean b = menuService.removeByIds(list);
        return Result.success();
    }

    @GetMapping("/page")
        public Result page(
                @RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum,
                @RequestParam(value = "pageSize",defaultValue = "100",required = false) int pageSize,
                Menu menu){

        return Result.success(menuService.roleMenus(pageNum,pageSize,menu));
    }


    @PostMapping("/save")
    public Result insert(@RequestBody Menu menu){
        boolean b = menuService.saveOrUpdateUser(menu);
        return Result.success();

    }



    @GetMapping("/getMenu")
    public Result getUserByName(String menuname){
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(menuname != null,Menu::getMenuName,menuname);
        Menu menu = menuService.getOne(wrapper);
        return Result.success(menu);
    }

}
